package com.bolt.support.spring.jpa.jpql;

import com.bolt.common.utils.StrUtil;

import java.io.Serializable;
import java.lang.reflect.Field;

/**
 * Created by Administrator on 2020/9/9.
 */
public class DynamicCondition implements Serializable {

    private final Ops operator;

    private final Ops connect;

    private final ColumnClause clause;

    private final String test;

    private final static String BLANK_STR_EXPRS = "{} != null and {} != ''";

    private final static String BLANK_OBJ_EXPRS = "{} != null ";


    public DynamicCondition(Ops connect, Ops operator, ColumnClause clause, String test) {
        this.operator = operator;
        this.connect = connect;
        this.clause = clause;
        this.test = test;
    }

    public DynamicCondition(Ops connect, Ops operator, ColumnClause clause) {
        this.operator = operator;
        this.connect = connect;
        this.clause = clause;
        Field propertyField = clause.getPropertyField();
        if (propertyField != null && propertyField.getType().isAssignableFrom(String.class)) {
            this.test = StrUtil.format(BLANK_STR_EXPRS, clause.getParamName(), clause.getParamName());
        } else {
            this.test = StrUtil.format(BLANK_OBJ_EXPRS, clause.getParamName());
        }
    }

    public Ops getOperator() {
        return operator;
    }

    public Ops getConnect() {
        return connect;
    }

    public ColumnClause getClause() {
        return clause;
    }

    public String getTest() {
        return test;
    }
}

 